import { createRouter, createWebHistory } from 'vue-router';
import axios from 'axios';
import MainLayout from '../layouts/MainLayout.vue';
// 统一使用 ../views 路径引入
import DelegationList from '../views/DelegationList.vue';
import Select from '../views/Select.vue';
import Wallet from '../views/Wallet.vue';
import Receive from '../views/Receive.vue';
import SendPackage from '../views/SendPackage.vue';
import BasicDetail from '../views/BasicDetail.vue';
import Publish from '../views/Publish.vue';
import ChatWindow from '../views/ChatWindow.vue';
import ChatList from '../views/ChatList.vue';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';


const routes = [
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: '',
        name: 'DelegationList',
        component: DelegationList
      },
      {
        path: 'message',
        name: 'ChatList',
        component: ChatList
      },
      {
        path: 'delegation/:id',
        name: 'DelegationDetail',
        component: () => import('../views/DelegationDetail.vue')
      }
    ]
  },
  // 将 DelegationDetail_R 移动到顶层路由，与DelegationDetail保持一致的结构
  {
    path: '/delegation_r/:id',
    name: 'DelegationDetail_R',
    component: () => import('../views/DelegationDetail_R.vue')
  },
  // 将 ChatWindow 移到这里，作为顶层路由
  {
    path: '/chat/:id',
    name: 'ChatWindow',
    component: ChatWindow
  },
  {
    path: '/select',
    name: 'Select',
    component: Select
  },
  {
    path: '/Publish',
    name: 'Publish',
    component: Publish
  },
  {
    path: '/Publish/address',
    name: 'Address',
    component: () => import('../views/profile/Address.vue')
  },
  {
    path: '/edit',
    component: BasicDetail
  },
  {
    path: '/Wallet',
    component: Wallet
  },
  {
    path: '/Receive',
    component: Receive
  },
  {
    path: '/SendPackage',
    component: SendPackage
  },
  {
    path: '/edit/avatar',
    name: 'EditAvatar',
    component: () => import('../views/profile/EditAvatar.vue')
  },
  {
    path: '/edit/username',
    name: 'EditUsername',
    component: () => import('../views/profile/EditUsername.vue')
  },
  {
    path: '/edit/password',
    name: 'EditPassword',
    component: () => import('../views/profile/EditPassword.vue')
  },
  {
    path: '/edit/address',
    name: 'EditAddress',
    component: () => import('../views/profile/EditAddress.vue')
  },
  {
    path: '/edit/notes',
    name: 'EditNotes',
    component: () => import('../views/profile/EditNotes.vue')
  },
  {
    path: '/edit/gender',
    name: 'EditGender',
    component: () => import('../views/profile/EditGender.vue')
  },
  {
    path: '/edit/address/update',
    name: 'AddressEdit',
    component: () => import('../views/profile/AddressEditForm.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 修改路由守卫逻辑
router.beforeEach(async (to, from, next) => {
  const userStr = localStorage.getItem('user')
  const token = localStorage.getItem('token')
  if (token && userStr) {
    try {
      const user = JSON.parse(userStr)
      if (user && user.uid) {
        const uid = parseInt(user.uid)
        const res = await axios.post('/api/auth/autoLogin',
          { token, uid },
          {
            headers: {
              'Authorization': token,
              'Content-Type': 'application/json'
            }
          }
        )
        if (res.data.code !== 0) {
          return next('/login')
        }
      }
    } catch (error) {
      return next('/login')
    }
  }
  next()
})

export default router